var $ = getApp(),  methods = require("../methods.js");

Page(Object.assign({}, methods, {

  /**
   * 页面的初始数据
   */
  data: {
      datalist: [
          {img:"https://api.youmiwanglo.com/applet/v2/start1.png",url:'https://www.baidu.com'}
      ],
      count: 5,//倒计时秒数
      show_button:false,//初始不显示跳过按钮
      time:null
  },

  _onLoad(options) {
    
  },
  onShow(){
    console.log(99999999)
    var guideFlag = this.getCache2("guideFlag") ;
    if(guideFlag){
      console.log(11112222)
      this.setCache2('guideFlag', 0,10000); //1000 1秒钟
      wx.navigateTo({
          url: '/pages/index/index'
      })
    }else{
      console.log(33333333)
      this.getStartPage();
    }
  },
  //清除定时器
  onUnload: function() {
  　　clearInterval(this.data.time)
  },
  setCache2: function(t, e, a) {
		var n = +new Date / 1e3,
		i = !0,
		o = {
			expire: a ? n + parseInt(a) : 0,
			value: e
		};
		try {
			wx.setStorageSync(t , o)
		} catch(t) {
			i = !1
		}
		return i
  },
  getCache2: function(t, e) {
		var a = +new Date / 1e3,
		n = "";
		a = parseInt(a);
		try { (n = wx.getStorageSync()).expire > a || 0 == n.expire ? n = n.value: (n = "", this.removeCache2(t))
		} catch(t) {
			n = void 0 === e ? "": e
		}
		return n = n || ""
  },
  removeCache2: function(t) {
		var e = !0;
		try {
			wx.removeStorageSync()
		} catch(t) {
			e = !1
		}
		return e
	},
  getStartPage(){
    var that = this;
    $.get("/article/getStartPage",{},function(res){
      //console.log(res.data.datalist)
      if(res.datalist.length>0){
        that.setData({datalist:res.datalist,count:res.count})
        that.countDown();
        that.show_button();
        
      }else{
        wx.navigateTo({
              url: '/pages/index/index'
        })
      }
    })
  },
  //延迟一秒显示跳转按钮
  show_button() {
      let that = this
      setTimeout(function() {
            that.setData({
                  show_button: true
            })
      }, 1000)
  },
  //倒计时计数
  countDown: function() {
      let that = this;
      let count = that.data.count;
      console.log(count);
      console.log(this.data.time)
      //防止执行了大于一次，在调用之前先清除一次
      clearInterval(that.data.time);
      that.setData({
        time:null
      })
      that.data.time = setInterval(function() {
        console.log(count)
            if (count > 0) {
                  count--
                  that.setData({
                        count: count
                  })
            } else {
              console.log(that.data.time)
                  that.setData({
                        count: count
                  })
                  clearInterval(that.data.time)
                  that.switchTab();
                  
            }
      }, 1000)
  },
  //跳转到 tabBar 页面，并关闭其他所有非 tabBar 页面
  switchTab() {
    console.log(this.data.time)
    //console.log(777888)
    clearInterval(this.data.time); //清除倒计时
    wx.navigateTo({
          url: '/pages/index/index'
    })
  },
  gotourl(e){
      var tourl = e.currentTarget.dataset.url;
      console.log(tourl)
      if (tourl.indexOf('url::') === 0) { //外部链接
        tourl = '/pages/base/webview?url=' + encodeURIComponent(tourl.slice(5))
      }
      if (tourl.indexOf('https://') === 0 || tourl.indexOf('www.') === 0) { //外部链接
        if (tourl.indexOf('www.') === 0) tourl = 'https://' + tourl
        tourl = '/pages/base/webview?url=' + encodeURIComponent(tourl)
      }
      clearInterval(this.data.time); //清除倒计时

      this.setCache2('guideFlag', 1);
      wx.navigateTo({
        url: tourl
      })
  } 
}));